package com.wanmait.myhouse.webController;

import com.wanmait.myhouse.dao.UserDAO;
import com.wanmait.myhouse.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
@RequestMapping("web")
public class WebRegisterController {
    @GetMapping("register")
    public String register(){
        return "web/register";
    }
    @PostMapping("isReName")//注册时验证用户名是否重复方法
    @ResponseBody
    public String isReName(User user){
        UserDAO userDAO = new UserDAO();
        User user1 = userDAO.FindByName(user);
        if(user1 == null){
            return "1";
        }
        else{
            return "0";
        }
    }
    @PostMapping("userRegister")
    @ResponseBody
    public String userRegister(User user){//把注册页面表单信息写到数据库
       UserDAO userDAO = new UserDAO();
       user.setUserBan(true);
       user.setUserHead("20250103161957.jpg");
       LocalDateTime localDateTime = LocalDateTime.now();
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       Timestamp time = Timestamp.valueOf(localDateTime.format(formatter));
       user.setUserRegtime(time);
       userDAO.add(user);
       return "1";//注册成功后重定向到登录controller
    }
}
